﻿/*
客户列表
*/

Ext.define('PT.view.customer.CustomersPanel', {
    extend: 'Ext.panel.Panel',
    ids: null,
    viewConfig: {
        stripeRows: true
    },
    initComponent: function () {

        var me = this;
       
        var gridstore = Ext.create('Ext.data.Store', {
            fields: [
                { name: 'id', type: 'int' }, 'name', 'position', 'sex', 'mobile', 'tele', 'qq', 'email', 'province', 'city', 'remarks', 'lnkCompany', 'lnkUser', 'privilege',  'createDate'
            ],
            proxy: {
                type: 'ajax',
                url: 'Customer',
                reader: {
                    type: 'json',
                    totalProperty: 'count',
                    root: 'rows'
                }
            }
        });

        var loadData = function () {

            var searchKey = Ext.getCmp('txt_customer_key').getValue();

            gridstore.on('beforeload', function (store, options) {

                var extraParams = {
                    searchkey: searchKey
                };

                Ext.apply(store.proxy.extraParams, extraParams);

            });

            gridstore.load({
                params: {
                    start: 0,
                    limit: 25
                }
            });

        };
        
        Ext.applyIf(me, {
            layout: 'border',
            items: [
				{
				    xtype: 'gridpanel',
				    region: 'center',
				    store: gridstore,
				    selModel: Ext.create('Ext.selection.CheckboxModel', { mode: 'SIMPLE' }),
				    columns: [
    				{ header: '姓名', dataIndex: 'name' },
    				{
    				    header: '性别', dataIndex: 'sex', renderer: function (v) {

    				        if (v == '0') {
    				            return '女士';
    				        } else {
    				            return '先生';
    				        }
    				    }
    				},
    				{ header: '职位', dataIndex: 'postion' , flex: 1},
    				{ header: '电话', dataIndex: 'mobile', hidden: false },
    				{
    				    header: '城市', dataIndex: 'city', renderer: function (v) {
        
    				        return v;
    				    }
    				},
                    { header: 'Email', dataIndex: 'email' },
        		    {
        		        header: '创建时间', dataIndex: 'createDate', flex: 1, renderer: function (v) {

        		            if (v!=null&&v.length == 21) {

        		                var d = new Date(parseInt(v.substr(6, 13)));

        		                return Ext.Date.format(d, 'Y-m-d H:i:s');

        		            } else {
        		                return v;
        		            }
        		        }
        		    },
                            				{
                            				    xtype: 'actioncolumn',
                            				    flex: 1,
                            				    items: [{
                            				        icon: 'images/icons/fam/information.png',
                            				        tooltip: '查看',
                            				        handler: function (grid, rowIndex, colIndex) {
                            				            var rec = grid.getStore().getAt(rowIndex);

                            				            Ext.create('PT.view.window.EditCustomerWindow', {
                            				                rec: rec,
                            				                listeners: {
                            				                    'beforedestroy': function () {
                            				                        loadData();
                            				                    }
                            				                }
                            				            }).show();

                            				        }
                            				    }
                            				    ]
                            				}
				    ],
				    dockedItems: [{
				        xtype : 'toolbar',
				        dock : 'top',
				        items : [ {
				            text : '添加',
				            tooltip : '添加客户',
				            iconCls : 'add',
				            handler : function() {

				                Ext.create('PT.view.window.EditCustomerWindow', {
				                    listeners:{'beforedestroy':function(){										
				                        loadData();
				                    }}
				                }).show();
							
				            }
				        },{
				            text : '删除',
				            tooltip : '删除客户',						
				            iconCls : 'del',
				            handler : function() {
				                var selModel = me.child('grid').getSelectionModel().getSelection();

				                if (selModel.length == 0) {

				                    alert("请选择要删除客户！");
				                    return;
				                }

				                var idsStr = '';
				                for (var i = 0; i < selModel.length; i++) {
				                    idsStr += selModel[i].data.id + ',';
				                }

				                Ext.Msg.show({
				                    title: '信息',
				                    msg: '确定要删除选中客户吗？',
				                    buttons: Ext.Msg.YESNO,
				                    fn: function (buttonId, text, opt) {

				                        if (buttonId == 'yes') {

				                            Ext.Ajax.request({
				                                url: '/Customer/Delete' ,
				                                params: {
				                                    ids: idsStr
				                                },
				                                success: function (response) {
				                                    var text = response.responseText;

				                                    var m = Ext.JSON.decode(text);

				                                    if (m.success) {
				                                        loadData();
				                                    } else {
				                                        alert(m.msg);
				                                    }

				                                }
				                            });
				                        }
				                    },
				                    icon: Ext.window.MessageBox.QUESTION
				                });

				            }
				        },'-', {
				            xtype: 'textfield',				          
                            width:300,
				            maxLength: 11,
				            id: 'txt_customer_key'
				        }, {
				            text: '查询',
				            tooltip: '根据关键字查询结果',
				            iconCls: 'search',
				            handler: function () {
                                /**
				                var searchKey = Ext.getCmp('txt_customer_key').getValue();

				                if (searchKey == '') {

				                    alert('请输入查询关键字！');
				                    return;
				                }
                                **/
				                loadData();
				            }
				        }]
				    },
				        {
				            xtype: 'pagingtoolbar',
				            store: gridstore,
				            dock: 'bottom',
				            displayInfo: true
				        }
				    ]
				}
            ]
        });
        me.callParent(arguments);

        loadData();
    }
});